/* ==================================================================   
 * Created [2009-4-27 下午11:32:55] by Jon.King 
 * ==================================================================  
 * TSS 
 * ================================================================== 
 * mailTo:jinpujun@hotmail.com
 * Copyright (c) Jon.King, 2009-2012 
 * ================================================================== 
 */

package com.jinhe.tss.component.dynproperty.entity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.UniqueConstraint;

import com.jinhe.tss.component.dynproperty.DynConstants;
import com.jinhe.tss.core.persistence.IEntity;
import com.jinhe.tss.core.web.dispaly.tree.ILevelTreeNode;
import com.jinhe.tss.core.web.dispaly.tree.TreeAttributesMap;
import com.jinhe.tss.core.web.dispaly.xform.IXForm;

/** 
 * <p> DynableEntity.java </p> 
 * 
 * 动态实体注册类。
 * 需要扩展为动态实体的实体类需要在本类里注册，声明为动态实体类。
 */
@Entity
@Table(name = "component_dyn_DynableEntity", 
        uniqueConstraints = { 
        @UniqueConstraint(name="MULTI_NAME_DYN_ENTITY", columnNames = { "parentId", "name" }) 
    })
@SequenceGenerator(name = "dyn_sequence", sequenceName = "dyn_sequence", initialValue = 1, allocationSize = 20)
public class DynableEntity implements IEntity, ILevelTreeNode, IXForm {
    
    public static final Long DYN_ROOT_ID = new Long(0);

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "dyn_sequence")
    private Long id;               
    private Long parentId = DYN_ROOT_ID; 
    private Integer seqNo ;   // 顺序号：用于排序
    
    private String className;  //可配置动态属性类的类名（包括package路径）；如果是组，该项为空
    private String entityCode; //对应的实体Entity的code，用来唯一标识不同动态实体；如果是组，该项为空
    private String name;       //可配置动态属性类的类的中文名字

    
    /** 属于该动态实体的所有Grid模板 */
    @Transient private List<PropertyDef> properties = new ArrayList<PropertyDef>(); 
    
    /** 属于该动态实体的所有XForm模板 */
    @Transient private List<XFormTemplate> xformTemplates = new ArrayList<XFormTemplate>(); 
    
    /** 属于该动态实体的所有Grid模板 */
    @Transient private List<GridTemplate> gridTemplates = new ArrayList<GridTemplate>(); 
 
    public List<GridTemplate> getGridTemplates() {
        return gridTemplates;
    }

    public List<PropertyDef> getProperties() {
        return properties;
    }

    public List<XFormTemplate> getXformTemplates() {
        return xformTemplates;
    }
    
    public boolean isGroup(){
        return this.entityCode == null;
    }
    
    public TreeAttributesMap getAttributes() {
        TreeAttributesMap map = new TreeAttributesMap(id, name);
        map.put("parentId", this.parentId);
        map.put("className", this.className);
        map.put("entityCode", this.entityCode);
        map.put("type", "" + (entityCode == null ? DynConstants.GROUP_TYPE : DynConstants.ENTITY_TYPE));
        map.put("icon", "images/entity" + (entityCode == null ? "_group" : "") + ".gif");
        return map;
    }
    public Map<String, Object> getAttributesForXForm() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", this.id);
        map.put("name", this.name);
        map.put("parentId", this.parentId);
        map.put("className", this.className);
        map.put("entityCode", this.entityCode);
        return map;
    }

    public Class<?> getParentClass() {
        return getClass();
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getParentId() {
        return parentId;
    }

    public void setParentId(Long parentId) {
        this.parentId = parentId;
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    public String getEntityCode() {
        return entityCode;
    }

    public void setEntityCode(String entityCode) {
        this.entityCode = entityCode;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getSeqNo() {
        return seqNo;
    }

    public void setSeqNo(Integer seqNo) {
        this.seqNo = seqNo;
    }
}

